package org.rsbot.script.randoms;

import java.awt.Point;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.util.Timer;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSInterfaceChild;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;

/*
 * @author Secret Spy
 * @version 1.1 - 04/16/11
 */
@ScriptManifest(authors = "Secret Spy", name = "CapnArnav", version = 1.1)
public class CapnArnav extends Random {

    private static final int[] ArnavChest = {42337, 42338};
    private static final int ArnavID = 2308;
    private static final int ExitPortal = 11369;
    private static final int[][] IFaceSolveIDs = {
        {7, 14, 21}, // Bowl \\
        {5, 12, 19}, // Ring \\
        {6, 13, 20}, // Coin \\
        {8, 15, 22}}; // Bar \\
    private static final int[][] Arrows = {{2, 3}, {9, 10}, {16, 17}};
    private static final int TalkIFace = 228;
    private static final int ChestIFaceParent = 185;
    private static final int ChestIFaceUnlock = 28;
    private static final int ChestIFaceCenter = 23;
    private int index = -1;
    Point p;

    private static enum STATE {
        OpenChest, Solve, Talk, Exit
    }

    @Override
    public boolean activateCondition() {
        final RSNPC captain = getNearestNPCByID(ArnavID);
        if (captain != null) {
            sleep(random(1500, 1600));
            RSObject portal = getNearestObjectByID(ExitPortal);
            return portal != null;
        }
        return false;
    }

    @Override
    public void onFinish() {
        index = -1;
    }

    private STATE getState() {
        if (getNearestObjectByID(ArnavChest[1]) != null) {
            return STATE.Exit;
        } else if (canContinue()
                || getInterface(TalkIFace) != null
                && getInterface(TalkIFace).isValid()) {
            return STATE.Talk;
        } else if (getInterface(ChestIFaceParent) == null
                || !getInterface(ChestIFaceParent).isValid()) {
            return STATE.OpenChest;
        } else {
            return STATE.Solve;
        }
    }

    @Override
    public int loop() {
        if (!activateCondition()) {
            return -1;
        }
        if (getMyPlayer().isMoving()) {
            return random(1000, 2000);
        }
        switch (getState()) {
            case Exit:
                RSObject portal = getNearestObjectByID(ExitPortal);
                if (portal != null) {
                    if (!portal.isOnScreen()) {
                        turnToObject(portal);
                    }
                    if (atObject(portal, "Enter")) {
                        return random(1000, 1300);
                    }
                }
                break;

            case OpenChest:
                RSObject chest = getNearestObjectByID(ArnavChest);
                if (chest != null) {
                    if (atObject(chest, "Open")) {
                        return random(1000, 1300);
                    }
                }
                break;

            case Talk:
                if (canContinue()) {
                    clickContinue();
                    return random(1500, 2000);
                }
                RSInterfaceChild okay = getInterface(TalkIFace, 3);
                if (okay != null && okay.isValid()) {
                    atInterface(okay);
                }
                return random(1500, 2000);

            case Solve:
                RSInterface solver = getInterface(ChestIFaceParent);
                if (solver != null && solver.isValid()) {
                    String s = solver.getComponent(32).getText();
                    if (s.contains("Bowl")) {
                        index = 0;
                    } else if (s.contains("Ring")) {
                        index = 1;
                    } else if (s.contains("Coin")) {
                        index = 2;
                    } else if (s.contains("Bar")) {
                        index = 3;
                    }
                    if (solved()) {
                        atInterface(ChestIFaceParent, ChestIFaceUnlock);
                        return random(600, 900);
                    }
                    RSInterfaceChild container = solver.getComponent(ChestIFaceCenter);
                    for (int i = 0; i < 3; i++) {
                        int rand = random(0, 100);
                        if (rand < 50) {
                            rand = 0;
                        } else if (rand >= 50) {
                            rand = 1;
                        }
                        RSInterfaceChild target = solver.getComponent(IFaceSolveIDs[index][i]);
                        RSInterfaceChild arrow = solver.getComponent(Arrows[i][rand]);
                        while (container.isValid()
                                && target.isValid()
                                && !container.getArea().contains(
                                new Point(target.getCenter().x + 15, target.getCenter().y)) && arrow.isValid()
                                && new Timer(10000).isRunning()) {
                            atInterface(arrow);
                            sleep(random(1000, 1200));
                        }
                    }
                }
        }
        return random(500, 800);
    }

    private boolean solved() {
        if (index == -1) {
            return false;
        }
        RSInterface solver = getInterface(ChestIFaceParent);
        if (solver != null && solver.isValid()) {
            RSInterfaceChild container = solver.getComponent(ChestIFaceCenter);

            Point p1 = solver.getComponent(IFaceSolveIDs[index][0]).getCenter();
            p1.setLocation(p1.x + 15, p1.y);
            Point p2 = solver.getComponent(IFaceSolveIDs[index][1]).getCenter();
            p2.setLocation(p2.x + 15, p1.y);
            Point p3 = solver.getComponent(IFaceSolveIDs[index][2]).getCenter();
            p3.setLocation(p3.x + 15, p1.y);
            return (container.getArea().contains(p1)
                    && container.getArea().contains(p2) && container.getArea().contains(p3));
        }
        return false;
    }
}
